using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using disc1.XML;

namespace DacTaHinhThuc
{
    public partial class InputWizardFrm : Form
    {
        private Parser m_parser;
        private Settings m_settings;
        private List<Parameter> m_listPara;
        private string sDT;
       
        #region properties
        public Parser M_Parser
        {
            get { return this.m_parser; }
        }
        public string SDT
        {
            get { return this.sDT; }
        }
        public List<Parameter> ListPara
        {
            get { return this.m_listPara; }
            set { this.m_listPara = value; }
        }
        
        #endregion

        public void UpdateListView()
        {
            listView1.Items.Clear();

            foreach (Parameter p in this.m_listPara)
            {
                ListViewItem item = new ListViewItem(p.Name);

                if (p.IsArr)
                {
                    item.SubItems.Add(p.Set + "*");
                }
                else
                {
                    if (p.IsSet)
                    {
                        item.SubItems.Add(p.Set + "-set");
                    }
                    else
                    {
                        item.SubItems.Add(p.Set);
                    }
                }               
                listView1.Items.Add(item);                
            }
        }

        public InputWizardFrm()
        {
            InitializeComponent();
            
            this.m_listPara = new List<Parameter>();
            this.m_parser = new Parser();
            this.m_settings = new Settings();
            this.m_settings = ObjectXMLSerializer<Settings>.Load("Settings.xml");

        }
        public void InitHightLightTextBox()
        {
            txtPreCondition.ListKeyWords = this.m_settings.KeywordDT;
            txtPreCondition.ColorKeyword = SettingFrm.GetColor(this.m_settings.ColorKeyword);

            txtPostCondition.ListKeyWords = this.m_settings.KeywordDT;
            txtPostCondition.ColorKeyword = SettingFrm.GetColor(this.m_settings.ColorKeyword);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            InsertParaFrm frm = new InsertParaFrm(this);
            frm.ShowDialog();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtNameFunc.Text.Length > 0 && txtNameResult.Text.Length > 0 && comboType.Text.Length > 0)
            {
                //kiem tra ten ham
                if (Checking.CheckNameFunction(txtNameFunc.Text))
                {
                    this.m_parser.NameMethod = txtNameFunc.Text;

                    //kiem tra ket qua tra ve
                    if (Checking.CheckNameFunction(txtNameResult.Text))
                    {
                        this.m_parser.Result.Name = txtNameResult.Text;

                        //xac dinh loai;
                        this.m_parser.Result.Set = comboType.Text;
                        if (radioArray.Checked)
                        {
                            this.m_parser.Result.IsArr = true;
                        }
                        if (radioSet.Checked)
                        {
                            this.m_parser.Result.IsSet = true;
                        }

                        this.m_parser.Result.SystemType = Mapping.MappingSetToSystemType(this.m_parser.Result.Set);
                        this.m_parser.Result.Type = Mapping.MappingSetToType(this.m_parser.Result.Set);

                        //luu lai danh sach tham so
                        this.m_parser.ArrPara = this.m_listPara.ToArray();

                        this.m_parser.PreCondition = this.m_parser.ParsePreCondition(txtPreCondition.Text);
                        this.m_parser.PostCondition = this.m_parser.ParsePostCondition(txtPostCondition.Text);

                        //format string de xuat
                        string s = "";
                        foreach (Parameter p in this.m_parser.ArrPara)
                        {
                            s += p.Name + ":" + p.Set + ",";
                        }
                        if (s != "")
                        {
                            s = s.Substring(0, s.Length - 1);
                        }

                        sDT = string.Format("{0}({1}){2}:{3}\npre\n{4}\npost\n{5}", this.m_parser.NameMethod, s, this.m_parser.Result.Name, this.m_parser.Result.Set, txtPreCondition.Text, txtPostCondition.Text);

                        //thoa het cac dk tren thi close form
                        this.DialogResult = DialogResult.OK;
                        this.Close();                       
                    }
                    else
                    {
                        MessageBox.Show("Ten cua ket qua tra ve khong hop le");
                    }
                }
                else
                {
                    MessageBox.Show("Ten ham khong hop le");
                }
            }
            else
            {
                MessageBox.Show("Xin nhap day du thong tin!");
            }
        }

        /// <summary>
        /// xoa tham so
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {            
            foreach (ListViewItem ii in listView1.SelectedItems)
            {
                listView1.Items.Remove(ii);                
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnAnd_Click(object sender, EventArgs e)
        {
            if (txtPreCondition.Focused)
            {
                txtPreCondition.Text += mySymbol.And;
            }
            if (txtPostCondition.Focused)
            {
                txtPostCondition.Text += mySymbol.And;
            }
        }

        private void btnOr_Click(object sender, EventArgs e)
        {
            if (txtPreCondition.Focused)
            {
                txtPreCondition.Text += mySymbol.Or;
            }
            if (txtPostCondition.Focused)
            {
                txtPostCondition.Text += mySymbol.Or;
            }
        }

        private void btnNot_Click(object sender, EventArgs e)
        {
            if (txtPreCondition.Focused)
            {
                txtPreCondition.Text += mySymbol.Not;
            }
            if (txtPostCondition.Focused)
            {
                txtPostCondition.Text += mySymbol.Not;
            }
        }

        private void btnExist_Click(object sender, EventArgs e)
        {
            if (txtPreCondition.Focused)
            {
                txtPreCondition.Text += mySymbol.Exist;
            }
            if (txtPostCondition.Focused)
            {
                txtPostCondition.Text += mySymbol.Exist;
            }
        }

        private void btnAll_Click(object sender, EventArgs e)
        {
            if (txtPreCondition.Focused)
            {
                txtPreCondition.Text += mySymbol.All;
            }
            if (txtPostCondition.Focused)
            {
                txtPostCondition.Text += mySymbol.All;
            }
        }
    }
}